package com.maxmlr.music;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.google.common.collect.Lists;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @Package com.maxmlr
 * @ClassName MysqlGenerator
 * @Description MybatisPlus代码生成器
 * @author max
 * @since 2018年12月15日 下午6:50:52 
 * @version 1.0
 * @Copyright Copyright (c) 2018: www.maxmlr.com.
 */
public class MysqlPlusGenerator {

    private static String packageName="/Users/maxiao/java/gitee/music";
    private static String projectName="com.maxmlr.music";
    private static String packageClass="admin";
    private static String authorName="max";
    private static String[] table=new String[]{"adm_role_menu","adm_user_role"};
//    private static String[] table=new String[]{"song","song_collection","special","singer_collection","singer_profiles"};
    private static String[] prefix=new String[]{"adm_"};
    private static File file = new File(packageName);
    private static String path = file.getAbsolutePath();
    
    private static DbType dbType=DbType.MYSQL;
    private static String url="jdbc:mysql://47.107.172.86:3306/musics?characterEncoding=utf8";
    private static String driverName="com.mysql.cj.jdbc.Driver";
    private static String username="maxiao";
    private static String password="cniavj";

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(path+"/src/main/java");//输出目录
        globalConfig.setFileOverride(true);// 是否覆盖文件
        globalConfig.setOpen(false);//生成后打开文件夹
        globalConfig.setEnableCache(false);// XML 二级缓存
        globalConfig.setAuthor(authorName);
        globalConfig.setKotlin(false);
        globalConfig.setSwagger2(true);
        globalConfig.setActiveRecord(true);// 开启 activeRecord 模式
        globalConfig.setBaseResultMap(true);// XML ResultMap
        globalConfig.setDateType(DateType.ONLY_DATE);//时间类型对应策略
        globalConfig.setBaseColumnList(true);// XML columList
//        globalConfig.setEntityName("");
        globalConfig.setMapperName("%sMapper");// 自定义文件命名，注意 %s 会自动填充表实体属性！
        globalConfig.setXmlName("%sMapper");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setControllerName("%sController");
        globalConfig.setIdType(IdType.AUTO);
        mpg.setGlobalConfig(globalConfig);

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(dbType);// 数据库类型
        dataSourceConfig.setSchemaName("");
//        dataSourceConfig.setTypeConvert(new ITypeConvert());
        dataSourceConfig.setUrl(url);
        dataSourceConfig.setDriverName(driverName);
        dataSourceConfig.setUsername(username);
        dataSourceConfig.setPassword(password);
        mpg.setDataSource(dataSourceConfig);

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setTablePrefix(prefix);
        strategyConfig.setInclude(table);
//        strategyConfig.setFieldPrefix(new ());
//        strategyConfig.setSuperEntityClass("");
        strategyConfig.setSuperControllerClass("com.maxmlr.music.common.base.BaseController");
        strategyConfig.setCapitalMode(false);// 全局大写命名
        strategyConfig.setSkipView(false);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategyConfig.setSuperMapperClass("");
        strategyConfig.setSuperServiceClass("com.maxmlr.music.common.base.BaseService");// 自定义 service 父类
        strategyConfig.setSuperServiceImplClass("com.maxmlr.music.common.base.BaseServiceImpl");// 自定义 service 实现类父类
        strategyConfig.setEntitySerialVersionUID(true);
        strategyConfig.setEntityColumnConstant(false);
        strategyConfig.setEntityBuilderModel(false);
        strategyConfig.setEntityLombokModel(true);//实体 是否为lombok模型
        strategyConfig.setEntityBooleanColumnRemoveIsPrefix(false);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setControllerMappingHyphenStyle(true);
        strategyConfig.setEntityTableFieldAnnotationEnable(true);
        strategyConfig.setVersionFieldName("");
//        strategyConfig.setTableFillList();
        strategyConfig.setEnableSqlFilter(false);
        mpg.setStrategy(strategyConfig);

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(projectName);// 自定义包路径
        packageConfig.setModuleName(packageClass);
        packageConfig.setEntity("entity");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setMapper("dao");
//        packageConfig.setXml("");
        packageConfig.setController("controller");
        mpg .setPackageInfo(packageConfig);


        List<FileOutConfig> fileOutConfigList = Lists.newArrayList();
        fileOutConfigList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            // 自定义输出文件目录
            @Override
            public String outputFile(TableInfo tableInfo) {
                return path+"/src/main/resources/mapper/"+ packageClass+"/"+ tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        String projectNamePath = projectName.replaceAll("\\.", "\\/");
        fileOutConfigList.add(new FileOutConfig("/templates/admin/controller.java.vm") {
            // 自定义输出文件目录
            @Override
            public String outputFile(TableInfo tableInfo) {
                return path + "/src/main/java/" + projectNamePath + "/" + packageClass + "/" +
                        packageConfig.getController() + "/Adm" + tableInfo.getControllerName() + ".java";
            }
        });

        String htmlPath = path + "/src/main/resources/templates/" + packageClass + "/" ;
        String jsPath = path + "/src/main/resources/static/js/appjs/" + packageClass + "/" ;

        fileOutConfigList.add(new FileOutConfig("/templates/admin/list.html.vm") {
            // 自定义输出文件目录
            @Override
            public String outputFile(TableInfo tableInfo) {
                return  htmlPath + tableInfo.getEntityPath() + "/" + tableInfo.getEntityPath() + ".html";
            }
        });
        fileOutConfigList.add(new FileOutConfig("/templates/admin/list.js.vm") {
            // 自定义输出文件目录
            @Override
            public String outputFile(TableInfo tableInfo) {
                return jsPath + tableInfo.getEntityPath() + "/" + tableInfo.getEntityPath() + ".js";
            }
        });
        fileOutConfigList.add(new FileOutConfig("/templates/admin/add.html.vm") {
            // 自定义输出文件目录
            @Override
            public String outputFile(TableInfo tableInfo) {
                return htmlPath + tableInfo.getEntityPath() + "/add.html";
            }
        });
        fileOutConfigList.add(new FileOutConfig("/templates/admin/add.js.vm") {
            // 自定义输出文件目录
            @Override
            public String outputFile(TableInfo tableInfo) {
                return jsPath + tableInfo.getEntityPath() + "/add.js";
            }
        });
        fileOutConfigList.add(new FileOutConfig("/templates/admin/edit.html.vm") {
            // 自定义输出文件目录
            @Override
            public String outputFile(TableInfo tableInfo) {
                return htmlPath + tableInfo.getEntityPath() + "/edit.html";
            }
        });
        fileOutConfigList.add(new FileOutConfig("/templates/admin/edit.js.vm") {
            // 自定义输出文件目录
            @Override
            public String outputFile(TableInfo tableInfo) {
                return jsPath + tableInfo.getEntityPath() + "/edit.js";
            }
        });

        mpg.setCfg(
                // 注入自定义配置，可以在 VM 中使用 cfg.abc 设置的值
                new InjectionConfig() {
                    @Override
                    public void initMap() {
                        Map<String, Object> map = new HashMap<>();
                        map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                        this.setMap(map);
                    }
                }.setFileOutConfigList(fileOutConfigList)
        );

        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setEntity("/templates/entity.java.vm");
        templateConfig.setService("/templates/service.java.vm");
        templateConfig.setServiceImpl("/templates/serviceImpl.java.vm");
        templateConfig.setMapper("/templates/mapper.java.vm");
        templateConfig.setXml(null);
        templateConfig.setController("/templates/controller.java.vm");
        mpg.setTemplate(templateConfig);

        // 执行生成
        mpg.execute();
    }

}
